--#include "data\config\equip\EquipGodCastConfig.lua" once

--[[
神铸装备、圣装升阶
]]
function EquipGodCastUpgrade( sysarg, equipPtr )
	--print("EquipGodCastUpgrade...")
	if not equipPtr then
		return
	end

	local exitFlag = Item.IsEquipInBodyOrBag( sysarg, equipPtr )
	if exitFlag == 0 then
		return
	end
	local isHero = 0
	if exitFlag == 2 then
		isHero = 1
	end

	local itemid = Item.getItemProperty( sysarg, equipPtr, Item.ipItemID, 0)
	local goldCastCfg = EquipGodCastCfg[itemid]
	if not goldCastCfg then
		return
	end

	if not goldCastCfg.newEquipId or goldCastCfg.newEquipId <= 0 then
		return
	end

	local itemType 		= Item.getItemProperty( sysarg, equipPtr, Item.ipItemType, 0)
	local equipStar  	= Item.getItemProperty( sysarg, equipPtr, Item.ipItemStrong, 0)				--强化星级
	local lostStar		= Item.getItemProperty( sysarg, equipPtr, Item.ipItemLostStar, 0)			--损失星级
	local equipInject  	= Item.getItemProperty( sysarg, equipPtr, Item.ipItemInjectLevel, 0)		--注灵等级
	local equipLuck 	= Item.getItemProperty( sysarg, equipPtr, Item.ipItemLuck, 0)				--幸运（诅咒）
	local itemBind		= Item.getItemProperty( sysarg, equipPtr, Item.ipItemBind, 0)

	--先加到背包再装备上，所以要检查背包格子数
	local gridCount = Item.getBagEmptyGridCount(sysarg)
	if gridCount < 1 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
		return
	end

	--升阶的消耗
	if not goldCastCfg.consumes then
		return
	end

	if not Item.canEquipTakeOn( sysarg, goldCastCfg.newEquipId, isHero ) then 		--不满足升级条件
		local needCircle = Item.getItemPropertyById( goldCastCfg.newEquipId, Item.ipItemNeedCircle) or 0
		local needLevel  = Item.getItemPropertyById( goldCastCfg.newEquipId, Item.ipItemActorLevel) or 0
		if needCircle <= 0 then  		--不要求转数，只要求等级
			Actor.sendTipmsg( sysarg, string.format(Lang.ScriptTips.EquipGodCast001, needLevel), ttFlyTip )
		elseif needLevel <= 1 then 		--要求转数，不要求等级
			Actor.sendTipmsg( sysarg, string.format(Lang.ScriptTips.EquipGodCast002, needCircle), ttFlyTip )
		else 							--要求转数+等级
			Actor.sendTipmsg( sysarg, string.format(Lang.ScriptTips.EquipGodCast003, needCircle, needLevel), ttFlyTip )
		end
		return
	end

	if not CheckConsumeCond( sysarg, goldCastCfg.consumes ) then
		return 
	end

	local newBindFlag = itemBind			
	if Item.isBasicEquip( itemType ) then		--基础装备升阶后强制为非绑
		newBindFlag = 0
	end
	local newEquipPtr = Item.createItem(goldCastCfg.newEquipId, 1, 0, 0, newBindFlag)			--创建装备实体，但此时尚未进入玩家背包
	if not newEquipPtr then
		return
	end

	if not DoConsumeCond( sysarg, goldCastCfg.consumes, 1, GameLog.clEquipFurnaceOpFee, Lang.ScriptTips.EquipLog008 ) then
		--Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipFurnace008, ttFlyTip)
		return
	end

	Item.transSmithAttrs( equipPtr, newEquipPtr )			--进行洗练属性转移，此时原装备必须存在
	--删掉原装备，先删身上，没有再删背包
	if Item.removeEquip( sysarg, equipPtr, Lang.ScriptTips.EquipLog016, GameLog.clEquipGodCastDelOld ) < 1 then
		if Item.removeItem( sysarg, equipPtr, 1, Lang.ScriptTips.EquipLog016, GameLog.clEquipGodCastDelOld ) < 1 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipGodCast004, ttFlyTip)
			return
		end
	end

	--为安全起见，先删除原装备，再把newEquipPtr加入玩家背包
	local addCount = Item.addItem( sysarg, newEquipPtr, Lang.ScriptTips.EquipLog015, GameLog.clEquipGodCastAdd, 1)
	if addCount < 1 then
		return
	end

	--装备在身上或背包里，才能设置属性
	Item.setItemProperty( sysarg, newEquipPtr, Item.ipItemStrong, equipStar)
	Item.setItemProperty( sysarg, newEquipPtr, Item.ipItemLostStar, lostStar) 
	Item.setItemProperty( sysarg, newEquipPtr, Item.ipItemInjectLevel, equipInject) 
	Item.setItemProperty( sysarg, newEquipPtr, Item.ipItemLuck, equipLuck)


	if exitFlag == 1 or exitFlag == 2 then		--目标装备穿在身上，则升阶后新装备也要穿在身上
		--[[这里，原装备已经删除成功了
		if not Item.equipItem( sysarg, newEquipPtr ) then
			Item.removeItem(sysarg, newEquipPtr, 1, "del in bag", GameLog.clEquipGodCastDelInBag) 
		end
		]]
		Item.equipItem( sysarg, newEquipPtr, isHero )			--原装备是穿在身上的，新装备也穿到身上
	end

	local actorName = Actor.getName( sysarg )
	local actorId 	= Actor.getActorId( sysarg )
	local newItemid = Item.getItemProperty( sysarg, newEquipPtr, Item.ipItemID, 0)
	System.sendCommonLog( GameLog.clEquipGodCastUpgrade, actorId, actorName, 0, newItemid, itemid, "", "", Lang.ScriptTips.EquipLog017 )
	SendEquipGodCastResult(sysarg, 1, 1)		--操作成功

	local itemLink = Item.getItemLinkMsg( newItemid, newEquipPtr )
	local msg = string.format( Lang.ScriptTips.EquipGodCast006, Actor.getName( sysarg ), itemLink )
	System.broadcastTipmsg( msg, ttScreenCenter + ttChatWindow )
end

--[[
神铸装备回收
]]
function EquipGodCastRecover(sysarg, args)
	local nIdx 		= args[2] or 0			-- 等级索引
	local itemCount = args[4]				-- 装备数量
	--print("EquipGodCastRecover， itemCount="..itemCount)
	local pos		= args[5]				-- 位置：0背包1寻宝仓库
	local packet 	= args[6]				-- 装备guid列表
	local equipItemPtr 	= {}   --存储物品指针用来最后删除
	local equipGuidList = {}  --把取出的每个物品的GUID储存,用来检测是否已存在一样的,有则是通过非法手段来进行
	local awardList = {}

	local allShieldValue 	= 0			--神盾值
	local allGodCastValue 	= 0			--神铸碎片
	local funList =
	{
		[0] =
		{
			getBagItemPtrByGuid = Item.getBagItemPtrByGuid,
			removeItemByPtr		= Actor.removeItemByPtr,
		},
		[1] =
		{
		    getBagItemPtrByGuid = Dmkj.getItemPtrByGuid,
			removeItemByPtr		= Dmkj.removeItemByPtr,
		}
	}

	local count = 0
	for i=1, itemCount do
		local itemGuid = DataPack.readDouble(packet)
		if not CheckItemGuidSame( equipGuidList, itemGuid ) then 
			return 
		end
		local itemPtr = funList[pos].getBagItemPtrByGuid( sysarg, itemGuid )
		if not itemPtr then
			return
		end
		
		-- 获取装备是否可回收属性
		local itemId 		= Item.getItemId(itemPtr)
		--print("EquipGodCastRecover， ->itemId="..itemId)
		local goldCastCfg 	= EquipGodCastCfg[itemId]
		if not goldCastCfg then
			return
		end

		if not goldCastCfg.recycleAwards then
			return
		end

		--[[
		for i, recycleAward1 in ipairs( goldCastCfg.recycleAwards ) do
			awardList = AddAwardToAwards( recycleAward1, awardList )
		end
		

		local awardsRecycle = DeepCopyFromObject( goldCastCfg.recycleAwards ) 			--必须要深拷贝 
		awardList = AddAwardsToAwards( awardsRecycle, awardList )
		]]
		--暂时支持这两种
		for i, award in ipairs( goldCastCfg.recycleAwards ) do
			if award.type == 0 and award.id == 4093 then				--神铸碎片
				allGodCastValue = allGodCastValue + award.count
			elseif award.type == 22 then
				allShieldValue = allShieldValue + award.count
			end
		end
		--print("EquipGodCastRecover， allGodCastValue="..allGodCastValue..", allShieldValue="..allShieldValue)

		-- 保存装备指针
		table.insert( equipGuidList, itemGuid )
		table.insert( equipItemPtr, itemPtr)
		count = count + 1
	end
	
	if count <= 0 then
		return
	end

	local award1 = { type=0, id = 4093, count=allGodCastValue, }
	local award2 = { type=22, id = 0, count=allShieldValue, }
	
	if allGodCastValue > 0 then
		table.insert( awardList, award1 )
	end

	if allShieldValue > 0 then
		table.insert( awardList, award2 )
	end

	if not IsBagGridEnough( sysarg, awardList ) then
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.EquipGodCast005, ttFlyTip )
		return
	end
	
	local recoveryCount = 0
	-- 删除要回收的装备
	local nResult = true
	for _, itemPtr in ipairs( equipItemPtr or {}) do
		local itemId = Item.getItemId(itemPtr)
		if funList[pos].removeItemByPtr(sysarg, itemPtr, 1, true, Lang.ScriptTips.EquipLog018, GameLog.clEquipGodCastRecycleDel) ~= 1 then
			nResult = false
			break
		end
		recoveryCount = recoveryCount + 1
	end
	
	if not nResult then			--必须全部装备删除成功
		return
	end

	--[[
	for k, award in pairs( awardList ) do
		print("EquipGodCastRecover, type="..award.type..", id="..award.id..", count="..award.count)
	end
	]]

	--获得奖励
	GiveCommonAward(sysarg, awardList, GameLog.clEquipGodCastRecycleAdd, Lang.ScriptTips.EquipLog019)
	--SendRecoveryAwardData(sysarg, 1, mtBindCoin, totalExp, totalChip1, totalChip2, recoveryCount)
	SendEquipGodCastResult(sysarg, 2, 1)		--操作成功

	local actorName = Actor.getName( sysarg )
	local actorId 	= Actor.getActorId( sysarg )
	System.sendCommonLog( GameLog.clEquipGodCastRecycle, actorId, actorName, itemCount, allGodCastValue, allShieldValue, tostring(nIdx),tostring(pos), 
		Lang.ScriptTips.EquipLog020 )

	--成就事件
	Actor.triggerAchieveEvent(sysarg, 45, 1, recoveryCount)
end

--神铸装备结果
function SendEquipGodCastResult(sysarg, opType, result)
	local netPack = DataPack.allocPacket(sysarg, enEquipSystemID, sEquipGodCastResult)
	if netPack then
		DataPack.writeChar(netPack,opType)
		DataPack.writeChar(netPack,result)
		DataPack.flush(netPack)
		--print("SendEquipGodCastResult, opType="..opType..", id="..result)
	end
end




